import sys  # 系统特定的参数和功能


from loginWindow import UiLogin
import pymongo
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
# from PyQt5.QtWidgets import QApplication,QMainWindow
from PyQt5.QtWidgets import *
from gms_history import Dialog1
from mainwork import Uimainwindow
from Nomiji import Nomiji
from PyQt5 import QtCore, QtGui, QtWidgets
from man import Stacked


class UserLoginWindow(QMainWindow, UiLogin):
    def __init__(self):
        super(UserLoginWindow, self).__init__()
        # self.setupUi(self)
        self.setupUi(self)
        #绑定按钮
        # self.btn_login.clicked.connect(self.mainpage)
        #绑定回车
        self.edt_password.returnPressed.connect(self.mainpage)
        self.edt_username.returnPressed.connect(self.mainpage)

    def mainpage(self):
        # self.form.hide()
        # Form1 = QtWidgets.Qdialog()
        # dia = Dialog1()
        # dia.setupUi(Form1)
        # From1.show()
        # Form1.exec_()
        # self.form.show()

        userName = self.edt_username.text()  # 获取文本框内容
        password = self.edt_password.text()

        if len(userName)==0:
            reply=QMessageBox.warning(self, "错误", "未输入用户名", QMessageBox.Yes)
            return

        elif len(password)==0:
            reply=QMessageBox.warning(self, "错误", "未输入密码", QMessageBox.Yes)
            return

        if self.PasswordCheck(userName,password) == True:
            self.hide()
            uimain.show()


        else:
            return
            # if (qw.QMessageBox.Yes == reply):
            #     print("[info] you clicked yes button!")
            # elif (qw.QMessageBox.No == reply):
            #     print("[info] you clicked no button!")

    def PasswordCheck(self,userName,password):

        # 创建数据库
        myclient = pymongo.MongoClient("47.93.58.98", 12000)  # host port
        mydb = myclient["nfca_db"]
        # 连接nfca_db数据库，账号密码认证
        db = myclient.nfca_db
        db.authenticate("nfca", "nfca")
        # 登录数据库
        myclient = myclient["nfca_db"]
        # 连接表
        myUser = myclient.user
        # print(user)
        # for item in user.find():
        #     print(item)


        mydoc = None

        try:
            mydoc_temp = myUser.find_one({'username': userName})
            if mydoc_temp:
                mydoc=mydoc_temp
            else:
                reply = QMessageBox.warning(self, "错误", "用户名不存在", QMessageBox.Yes)
        except Exception as e:
            reply = QMessageBox.warning(self, "错误", str(e), QMessageBox.Yes)
        if mydoc:
            if password == mydoc["password"]:
                return True
            else:
                # 输出用户名或密码错误窗口
                reply = QMessageBox.warning(self, "错误", "密码错误", QMessageBox.Yes)
                return False

if __name__ == '__main__':
    app = QApplication(sys.argv)

    myWin = UserLoginWindow()  # 账号密码的登陆界面
    dia = Dialog1()  # 历史数据管理的界面
    uimain = Uimainwindow()  # 主界面点击跳转
    e_chart = Stacked()     #数据图表界面
    nomi = Nomiji()  # 浓密机界面

    # 登录界面跳转按钮实例化
    btn_login = myWin.btn_login

    btn_login.clicked.connect(myWin.mainpage)

    b1 = uimain.actionasdasd
    b1.triggered.connect(dia.show)
    b1.triggered.connect(dia.setTableView)
    with open('./ElegantDark.qss') as file:
        qss = file.readlines()
        qss = ''.join(qss).strip('\n')
    myWin.setStyleSheet(qss)
    uimain.setStyleSheet(qss)
    nomi.setStyleSheet(qss)
    dia.setStyleSheet(qss)
    b2 = uimain.actionasdasd_2
    b2.triggered.connect(e_chart.show)

    b3 = uimain.actionquanjugailan
    b3.triggered.connect(nomi.show)

    myWin.show()
    sys.exit(app.exec_())


# if __name__ == '__main__':  # 自身模块（__name__）的值是__name__
#     # app = QApplication(sys.argv)#创建QApplication的对象app，传参
#     #                             #sys.argv 传递给Python脚本的命令行参数列表
#     # mainWindow = QMainWindow()  #创建QMainWindow对象mainWindow
#     # ui = login.Ui_login()  #向主窗口添加控件
#     # ui.setupUi(mainWindow)      #调用方法
#     # mainWindow.show()
#
#     app = QApplication(sys.argv)
#     MainWindow = QtWidgets.QWidget()
#
#     ui = UserLoginWindow()
#     dia = Dialog1()
#     uimain = Uimainwindow()
#
#     btn = uimain.actionasdasd
#     btn.triggered.connect(dia.show)
#
#     ui.setupUi(MainWindow)
#     MainWindow.show()
#     sys.exit(app.exec_())

